﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Plumbing.ParamForm;
using YArchitech.Plumbing.Properties;

namespace YArchitech.Plumbing
{
	public partial class MainCalculationForm : System.Windows.Forms.Form
	{
		public MainCalculationForm(bool drawEnabled)
		{
			this.InitializeComponent();
            //base.Icon = Resource.YArchitechIcon;
			string xmlPath = Path.Combine(Product.UserDataLocation, "GeothermalCoil.xml");
			this.InitializeValue(xmlPath);
			this.toolDraw.Enabled = drawEnabled;
			this.menuItemDraw.Enabled = drawEnabled;
		}

		private void toolOpen_Click(object sender, EventArgs e)
		{
			if (this.bSave)
			{
				DialogResult dialogResult = YJKMessageBox.Show("是否保存数据文件？", MessageBoxButtons.YesNoCancel);
				if (dialogResult != DialogResult.Cancel)
				{
					if (dialogResult == DialogResult.Yes)
					{
						this.toolSave_Click(sender, e);
						this.OpenXml();
						return;
					}
					if (dialogResult != DialogResult.No)
					{
						return;
					}
					this.OpenXml();
					return;
				}
			}
			else
			{
				this.OpenXml();
			}
		}

		private void OpenXml()
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.Filter = "数据文件|*.xml";
			openFileDialog.RestoreDirectory = true;
			openFileDialog.FilterIndex = 1;
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				string fileName = openFileDialog.FileName;
				this.InitializeValue(fileName);
			}
		}

		private void toolSave_Click(object sender, EventArgs e)
		{
			this.dataGridView.EndEdit();
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.Filter = "数据文件|*.xml";
			saveFileDialog.RestoreDirectory = true;
			saveFileDialog.FilterIndex = 1;
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				string fileName = saveFileDialog.FileName;
				if (this.systemDataOperation.SaveXml(fileName, this.option, this.buildSystem))
				{
					YJKMessageBox.Information("保存成功！");
					this.bKeepForm = false;
					return;
				}
			}
			else
			{
				this.bKeepForm = true;
			}
		}

		private void toolHelp_Click(object sender, EventArgs e)
		{
			new OperationHelpForm().ShowDialog();
		}

		private void menuItemExit_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void MainCalculationForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult != DialogResult.Abort)
			{
				if (this.bSave)
				{
					DialogResult dialogResult = YJKMessageBox.Show("是否保存数据文件？", MessageBoxButtons.YesNoCancel);
					if (dialogResult == DialogResult.Cancel)
					{
						e.Cancel = true;
						return;
					}
					if (dialogResult != DialogResult.Yes)
					{
						if (dialogResult != DialogResult.No)
						{
							return;
						}
						base.Dispose();
						return;
					}
					else
					{
						this.toolSave_Click(sender, e);
						if (this.bKeepForm)
						{
							e.Cancel = true;
							return;
						}
					}
				}
				else
				{
					base.Dispose();
				}
			}
		}

		private void toolSystem_Click(object sender, EventArgs e)
		{
			this.dataGridView.EndEdit();
			SystemSettingForm systemSettingForm = new SystemSettingForm(this.option);
			systemSettingForm.ShowDialog();
			if (systemSettingForm.DialogResult == DialogResult.OK)
			{
				this.buildSystem.SetConnStyleAndTemper(this.option);
				this.buildSystem.CalcRiserPipeLength(this.option);
				this.BindTreeView();
				this.bSave = true;
			}
		}

		private void toolHeight_Click(object sender, EventArgs e)
		{
			this.dataGridView.EndEdit();
			FloorHeightForm floorHeightForm = new FloorHeightForm(this.option, this.buildSystem);
			floorHeightForm.ShowDialog();
			if (floorHeightForm.DialogResult == DialogResult.OK)
			{
				this.buildSystem.CalcRiserPipeLength(this.option);
				this.BindTreeView();
				this.bSave = true;
			}
		}

		private void toolAdvanced_Click(object sender, EventArgs e)
		{
			this.dataGridView.EndEdit();
			AdvancedSettingForm advancedSettingForm = new AdvancedSettingForm(this.option);
			advancedSettingForm.ShowDialog();
			if (advancedSettingForm.DialogResult == DialogResult.OK)
			{
				this.BindTreeView();
				this.bSave = true;
			}
		}

		private void toolAdd_Click(object sender, EventArgs e)
		{
			this.dataGridView.EndEdit();
			new DataHelper(this.treeView.SelectedNode, this.buildSystem).AddData();
			this.BindTreeView();
			this.bSave = true;
		}

		private void toolDelete_Click(object sender, EventArgs e)
		{
			this.dataGridView.EndEdit();
			new DataHelper(this.treeView.SelectedNode, this.buildSystem).DeleteData();
			this.BindTreeView();
			this.bSave = true;
		}

		private void toolCopy_Click(object sender, EventArgs e)
		{
			this.dataGridView.EndEdit();
			CopyForm copyForm = new CopyForm(this.treeView.SelectedNode, this.buildSystem);
			copyForm.ShowDialog();
			if (copyForm.DialogResult == DialogResult.OK)
			{
				this.BindTreeView();
				this.bSave = true;
			}
		}

		private void toolModify_Click(object sender, EventArgs e)
		{
			this.dataGridView.EndEdit();
			DataModifyForm dataModifyForm = new DataModifyForm(new DataHelper(this.treeView.SelectedNode, this.buildSystem).GetSelectClass());
			dataModifyForm.ShowDialog();
			if (dataModifyForm.DialogResult == DialogResult.OK)
			{
				this.BindTreeView();
				this.bSave = true;
			}
		}

		private void toolDesignCalc_Click(object sender, EventArgs e)
		{
			this.dataGridView.EndEdit();
			GeothermalSystemCalc geothermalSystemCalc = new GeothermalSystemCalc(this.option, this.buildSystem, true);
			geothermalSystemCalc.DesignCalc();
			this.buildSystem = geothermalSystemCalc.BuildSystem;
			this.BindTreeView();
			this.menuItemCheckCalc.Enabled = true;
			this.toolCheckCalc.Enabled = true;
		}

		private void toolCheckCalc_Click(object sender, EventArgs e)
		{
			this.dataGridView.EndEdit();
			GeothermalSystemCalc geothermalSystemCalc = new GeothermalSystemCalc(this.option, this.buildSystem, false);
			geothermalSystemCalc.DesignCalc();
			this.buildSystem = geothermalSystemCalc.BuildSystem;
			this.BindTreeView();
		}

		private void toolExcel_Click(object sender, EventArgs e)
		{
			this.dataGridView.EndEdit();
			new ExportExcelProgessBar(this.buildSystem, this.option).ShowDialog();
		}

		private void toolDraw_Click(object sender, EventArgs e)
		{
			this.dataGridView.EndEdit();
			base.DialogResult = DialogResult.Abort;
		}

		private void menuItemDiameterTable_Click(object sender, EventArgs e)
		{
			new DiameterTableForm().ShowDialog();
		}

		private void InitializeValue(string xmlPath)
		{
			this.option = new OptionData();
			this.systemDataOperation = new SystemDataOperation(xmlPath);
			if (!this.systemDataOperation.CheckXmlFile())
			{
				YJKMessageBox.Show("选择文件不正确，请选择地热计算保存的数据文件！");
				return;
			}
			this.option = this.systemDataOperation.GetOptionData();
			this.buildSystem = new InBuildSystem(this.systemDataOperation.GetBuildSystem(this.option));
			this.buildSystem.SetConnStyleAndTemper(this.option);
			this.buildSystem.ReckonFittingOnPipe();
			this.BindTreeView();
			this.BindDataGrid(this.listMain);
		}

		private void BindTreeView()
		{
			TreeNode selectedNode = this.treeView.SelectedNode;
			this.buildSystem.ReckonFittingOnPipe();
			this.treeView.Nodes.Clear();
			TreeNode treeNode = new TreeNode("室内系统")
			{
				Tag = "listMain"
			};
			this.treeView.Nodes.Add(treeNode);
			this.treeView.SelectedNode = treeNode;
			TreeNode treeNode2 = new TreeNode("总供水干管")
			{
				Tag = "listMainPipeHeader"
			};
			TreeNode treeNode3 = new TreeNode("总回水干管")
			{
				Tag = "listMainPipeHeader"
			};
			treeNode.Nodes.Add(treeNode2);
			treeNode.Nodes.Add(treeNode3);
			if (selectedNode != null && selectedNode.Text == treeNode2.Text)
			{
				this.treeView.SelectedNode = treeNode2;
			}
			if (selectedNode != null && selectedNode.Text == treeNode3.Text)
			{
				this.treeView.SelectedNode = treeNode3;
			}
			int num = 1;
			foreach (ShareRPipeSystem shareRPipeSystem in this.buildSystem.SrpSystems)
			{
				TreeNode treeNode4 = new TreeNode("共用立管 " + num)
				{
					Tag = "listShareRPipe"
				};
				treeNode.Nodes.Add(treeNode4);
				TreeNode treeNode5 = new TreeNode("供水立管")
				{
					Tag = "listMainPipeHeader"
				};
				TreeNode treeNode6 = new TreeNode("回水立管")
				{
					Tag = "listMainPipeHeader"
				};
				treeNode4.Nodes.Add(treeNode5);
				treeNode4.Nodes.Add(treeNode6);
				if (selectedNode != null && selectedNode.Parent != null && selectedNode.Text == treeNode5.Text && selectedNode.Parent.Text == treeNode4.Text)
				{
					this.treeView.SelectedNode = treeNode5;
				}
				if (selectedNode != null && selectedNode.Parent != null && selectedNode.Text == treeNode6.Text && selectedNode.Parent.Text == treeNode4.Text)
				{
					this.treeView.SelectedNode = treeNode6;
				}
				int num2 = 1;
				foreach (FloorSystem floorSystem in shareRPipeSystem.FloorSystems)
				{
					TreeNode treeNode7 = new TreeNode("楼层 " + num2)
					{
						Tag = "listFloor"
					};
					treeNode4.Nodes.Add(treeNode7);
					int num3 = 1;
					foreach (IndoorSystem indoorSystem in floorSystem.IndoorSystems)
					{
						TreeNode treeNode8 = new TreeNode("户内系统 " + num3)
						{
							Tag = "listInDoor"
						};
						treeNode7.Nodes.Add(treeNode8);
						if (indoorSystem.CirclePipes.Count > 1)
						{
							TreeNode treeNode9 = new TreeNode("户内供水干管")
							{
								Tag = "listInDoorPipe"
							};
							TreeNode treeNode10 = new TreeNode("户内回水干管")
							{
								Tag = "listInDoorPipe"
							};
							treeNode8.Nodes.Add(treeNode9);
							treeNode8.Nodes.Add(treeNode10);
							if (selectedNode != null && selectedNode.Parent != null && selectedNode.Parent.Parent != null && selectedNode.Parent.Parent.Parent != null && selectedNode.Text == treeNode9.Text && treeNode9.Parent.Text == selectedNode.Parent.Text && treeNode9.Parent.Parent.Text == selectedNode.Parent.Parent.Text && treeNode9.Parent.Parent.Parent.Text == selectedNode.Parent.Parent.Parent.Text)
							{
								this.treeView.SelectedNode = treeNode9;
							}
							if (selectedNode != null && selectedNode.Parent != null && selectedNode.Parent.Parent != null && selectedNode.Parent.Parent.Parent != null && selectedNode.Text == treeNode10.Text && treeNode10.Parent.Text == selectedNode.Parent.Text && treeNode10.Parent.Parent.Text == selectedNode.Parent.Parent.Text && treeNode10.Parent.Parent.Parent.Text == selectedNode.Parent.Parent.Parent.Text)
							{
								this.treeView.SelectedNode = treeNode10;
							}
						}
						int num4 = 1;
						foreach (CirclePipe circlePipe in indoorSystem.CirclePipes)
						{
							TreeNode treeNode11 = new TreeNode("环路 " + num4)
							{
								Tag = "listCirclePipe"
							};
							treeNode8.Nodes.Add(treeNode11);
							num4++;
							if (selectedNode != null && selectedNode.Parent != null && selectedNode.Parent.Parent != null && selectedNode.Parent.Parent.Parent != null && selectedNode.Text == treeNode11.Text && treeNode11.Parent.Text == selectedNode.Parent.Text && treeNode11.Parent.Parent.Text == selectedNode.Parent.Parent.Text && treeNode11.Parent.Parent.Parent.Text == selectedNode.Parent.Parent.Parent.Text)
							{
								this.treeView.SelectedNode = treeNode11;
							}
						}
						num3++;
						if (selectedNode != null && selectedNode.Parent != null && selectedNode.Parent.Parent != null && selectedNode.Text == treeNode8.Text && treeNode8.Parent.Text == selectedNode.Parent.Text && treeNode8.Parent.Parent.Text == selectedNode.Parent.Parent.Text)
						{
							this.treeView.SelectedNode = treeNode8;
						}
					}
					num2++;
					if (selectedNode != null && selectedNode.Parent != null && selectedNode.Text == treeNode7.Text && treeNode7.Parent.Text == selectedNode.Parent.Text)
					{
						this.treeView.SelectedNode = treeNode7;
					}
				}
				num++;
				if (selectedNode != null && selectedNode.Text == treeNode4.Text)
				{
					this.treeView.SelectedNode = treeNode4;
				}
			}
			treeNode.Expand();
		}

		private void BindDataGrid(IList<string> listHeader)
		{
			DataHelper dataHelper = new DataHelper(this.treeView.SelectedNode, this.buildSystem);
			this.dataGridView.Columns.Clear();
			dataHelper.BindDataGridHeader(listHeader, this.dataGridView);
			dataHelper.BindDataGridData(this.dataGridView);
			this.BindDataGridRightClick(listHeader);
		}

		private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
		{
			TreeNode node = e.Node;
			IList<string> listHeader = this.GetlistHeader();
			this.BindDataGrid(listHeader);
			this.GetButtonEnabled(node);
		}

		private IList<string> GetlistHeader()
		{
			IList<string> result = new string[0];
			string text = this.treeView.SelectedNode.Tag.ToString();
			uint num = YArchitech.Revit.PrivateImplementationDetails.ComputeStringHash(text);
			if (num <= 857442589u)
			{
				if (num != 262276377u)
				{
					if (num != 787004638u)
					{
						if (num == 857442589u)
						{
							if (text == "listMainPipeHeader")
							{
								result = this.listMainPipeHeader;
							}
						}
					}
					else if (text == "listInDoor")
					{
						result = this.listInDoor;
					}
				}
				else if (text == "listCirclePipe")
				{
					result = this.listCirclePipe;
				}
			}
			else if (num <= 2168334097u)
			{
				if (num != 1291941034u)
				{
					if (num == 2168334097u)
					{
						if (text == "listFloor")
						{
							result = this.listFloor;
						}
					}
				}
				else if (text == "listInDoorPipe")
				{
					result = this.listInDoorPipe;
				}
			}
			else if (num != 2169576422u)
			{
				if (num == 2872028484u)
				{
					if (text == "listMain")
					{
						result = this.listMain;
					}
				}
			}
			else if (text == "listShareRPipe")
			{
				result = this.listShareRPipe;
			}
			return result;
		}

		private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			new DataHelper(this.treeView.SelectedNode, this.buildSystem).SaveCellData(e, this.dataGridView);
		}

		private void BindDataGridRightClick(IList<string> listHeader)
		{
			this.menuShowItem.DropDownItems.Clear();
			int num = 0;
			foreach (string text in listHeader)
			{
				if (num != 0)
				{
					ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem(text);
					toolStripMenuItem.Checked = true;
					toolStripMenuItem.Click += this.menuItem_Click;
					this.menuShowItem.DropDownItems.Add(toolStripMenuItem);
				}
				num++;
			}
			this.cmsDataGridView.Items.Add(this.menuHideItem);
			this.cmsDataGridView.Items.Add(this.menuShowItem);
			this.menuHideItem.Click += this.menuHideItem_Click;
		}

		private void menuHideItem_Click(object sender, EventArgs e)
		{
			string headerText = this.dataGridView.Columns[this.iColumnIndex].HeaderText;
			this.dataGridView.Columns[this.iColumnIndex].Visible = false;
			foreach (object obj in this.menuShowItem.DropDownItems)
			{
				ToolStripMenuItem toolStripMenuItem = obj as ToolStripMenuItem;
				if (toolStripMenuItem != null && toolStripMenuItem.Text == headerText)
				{
					toolStripMenuItem.Checked = false;
				}
			}
		}

		private void menuItem_Click(object sender, EventArgs e)
		{
			ToolStripMenuItem toolStripMenuItem = sender as ToolStripMenuItem;
			if (toolStripMenuItem != null)
			{
				if (toolStripMenuItem.Checked)
				{
					toolStripMenuItem.Checked = false;
					DataGridViewColumn dataGridViewColumn = this.dataGridView.Columns[toolStripMenuItem.Text];
					if (dataGridViewColumn != null)
					{
						dataGridViewColumn.Visible = false;
						return;
					}
				}
				else
				{
					toolStripMenuItem.Checked = true;
					DataGridViewColumn dataGridViewColumn2 = this.dataGridView.Columns[toolStripMenuItem.Text];
					if (dataGridViewColumn2 != null)
					{
						dataGridViewColumn2.Visible = true;
					}
				}
			}
		}

		private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			DataHelper dataHelper = new DataHelper(this.treeView.SelectedNode, this.buildSystem);
			object selectClass = dataHelper.GetSelectClass();
			if (e.RowIndex == -1)
			{
				return;
			}
			DataGridViewColumn dataGridViewColumn = this.dataGridView.Columns["ξ"];
			if (dataGridViewColumn != null && e.ColumnIndex == dataGridViewColumn.Index)
			{
				HYPipeData hyPipeData = null;
				if (selectClass is IList<HYPipeData>)
				{
					hyPipeData = ((IList<HYPipeData>)selectClass)[e.RowIndex];
				}
				else if (selectClass is CirclePipe)
				{
					hyPipeData = ((CirclePipe)selectClass).PipeData;
				}
				else if (selectClass is HYPipeData)
				{
					hyPipeData = (HYPipeData)selectClass;
				}
				LocalResistanceForm localResistanceForm = new LocalResistanceForm(hyPipeData);
				localResistanceForm.ShowDialog();
				if (localResistanceForm.DialogResult == DialogResult.OK)
				{
					dataHelper.BindDataGridData(this.dataGridView);
				}
			}
		}

		private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			DataGridViewColumn dataGridViewColumn = this.dataGridView.Columns["L(m)"];
			DataGridViewColumn dataGridViewColumn2 = this.dataGridView.Columns["Q"];
			if (dataGridViewColumn != null && this.dataGridView.CurrentCell.ColumnIndex == dataGridViewColumn.Index)
			{
				e.Control.KeyPress += this.dataGridViewCell_KeyPress;
			}
			if (dataGridViewColumn2 != null && this.dataGridView.CurrentCell.ColumnIndex == dataGridViewColumn2.Index)
			{
				e.Control.KeyPress += this.dataGridViewCell_KeyPress;
			}
		}

		private void dataGridViewCell_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar != '\b' && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
			{
				e.Handled = true;
			}
		}

		private void dataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
		{
			if (e.Button == MouseButtons.Right && e.RowIndex == -1)
			{
				this.iColumnIndex = e.ColumnIndex;
				if (this.iColumnIndex == 0)
				{
					this.cmsDataGridView.Items.Remove(this.menuHideItem);
				}
				else
				{
					this.cmsDataGridView.Items.Insert(0, this.menuHideItem);
				}
				this.cmsDataGridView.Show(Control.MousePosition.X, Control.MousePosition.Y);
			}
		}

		private void GetButtonEnabled(TreeNode treeNode)
		{
			if (treeNode.Text == "室内系统" || treeNode.Text.Contains("干管") || treeNode.Text.Contains("供水立管") || treeNode.Text.Contains("回水立管"))
			{
				this.btnCopy.Enabled = false;
				this.btnModify.Enabled = false;
				this.BtnDel.Enabled = false;
				this.toolCopy.Enabled = false;
				this.toolModify.Enabled = false;
				this.toolDelete.Enabled = false;
				this.toolAdd.Enabled = false;
				this.menuItemCopy.Enabled = false;
				this.menuItemModify.Enabled = false;
				this.menuItemDelete.Enabled = false;
				this.menuItemAdd.Enabled = false;
				this.toolTreeAdd.Enabled = false;
				this.toolTreeCopy.Enabled = false;
				this.toolTreeDel.Enabled = false;
				this.toolTreeModify.Enabled = false;
				return;
			}
			this.btnCopy.Enabled = true;
			this.btnModify.Enabled = true;
			this.BtnDel.Enabled = true;
			this.toolCopy.Enabled = true;
			this.toolModify.Enabled = true;
			this.toolDelete.Enabled = true;
			this.toolAdd.Enabled = true;
			this.menuItemCopy.Enabled = true;
			this.menuItemModify.Enabled = true;
			this.menuItemDelete.Enabled = true;
			this.menuItemAdd.Enabled = true;
			this.toolTreeAdd.Enabled = true;
			this.toolTreeCopy.Enabled = true;
			this.toolTreeDel.Enabled = true;
			this.toolTreeModify.Enabled = true;
		}

		private void treeView_MouseDown(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Right)
			{
				TreeNode nodeAt = this.treeView.GetNodeAt(e.X, e.Y);
				if (nodeAt != null)
				{
					this.treeView.SelectedNode = nodeAt;
				}
				this.cmsTreeView.Show(Control.MousePosition.X, Control.MousePosition.Y);
			}
		}

		private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
		{
			this.bSave = true;
		}

		private void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
		{
			this.bSave = true;
		}

		private IList<string> listMain = new string[]
		{
			"共用立管数",
			"总负荷(W)",
			"总流量(kg/h)",
			"总压降(Pa)"
		};

		private IList<string> listMainPipeHeader = new string[]
		{
			"管段编号",
			"G(kg/h)",
			"公称直径",
			"L(m)",
			"ξ",
			"V(m/s)",
			"R(Pa/m)",
			"ΔPy(Pa)",
			"ΔPj(Pa)",
			"ΔP(Pa)"
		};

		private IList<string> listInDoorPipe = new string[]
		{
			"管段编号",
			"G(kg/h)",
			"公称外径",
			"L(m)",
			"ξ",
			"V(m/s)",
			"R(Pa/m)",
			"ΔP(Pa)",
			"不平衡率(%)"
		};

		private IList<string> listCirclePipe = new string[]
		{
			"管段编号",
			"Q",
			"G(kg/h)",
			"公称外径",
			"L(m)",
			"ξ",
			"V(m/s)",
			"R(Pa/m)",
			"ΔP(Pa)",
			"不平衡率(%)"
		};

		private IList<string> listShareRPipe = new string[]
		{
			"楼层数",
			"不平衡率(%)"
		};

		private IList<string> listFloor = new string[]
		{
			"户内系统数",
			"不平衡率(%)"
		};

		private IList<string> listInDoor = new string[]
		{
			"环路数",
			"不平衡率(%)",
			"控制阀资用压力(Pa)",
			"控制阀控制压差(Pa)"
		};

		private int iColumnIndex;

		public InBuildSystem buildSystem;

		public OptionData option;

		private SystemDataOperation systemDataOperation;

		private bool bKeepForm;

		private bool bSave;
	}
}
